package com.lnsoft.cac.server.handle.codec.frameMessage;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.List;


@Data
/**
 * 监测数据--日/月 统计数据
 * */
public class VoltageDayMon_Msg {
    private int Measure_Point_ID;                       //1byte     监测点号（无符号整数）
    private int MeasureData_Num;                        //1byte     监测数据个数（无符号整数）
    private List<VoltageDayMon_Data> MeasureData;      //          统计条目

    public ByteBuf toByteBuf() throws Exception{
        if (!Util.measurePointIDIsAvailable(Measure_Point_ID)) {
            throw new Exception("监测点号设置异常");
        }
        if (!Util.measureDataNumIsAvailable(MeasureData_Num)) {
            throw new Exception("监测数据个数设置异常");
        }
        if (MeasureData == null) {
            MeasureData = new ArrayList<>();
        }
        if (MeasureData_Num != MeasureData.size()) {
            throw new Exception("监测数据个数和统计条目数不一致");
        }
        ByteBuf bf = Unpooled.buffer(1453);
        bf.writeByte(Measure_Point_ID);
        bf.writeByte(MeasureData_Num);
        for (VoltageDayMon_Data item : MeasureData) {
            bf.writeBytes(item.toByteBuf());
        }
        return bf;
    }


    //  解析 [检测数据-日/月统计数据] 帧报文内容区 到消息体中
    public static VoltageDayMon_Msg decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        VoltageDayMon_Msg msg = new VoltageDayMon_Msg();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        int num = bf.getUnsignedByte(1);
//        if ((num * 41 + 2) != len) {
//            throw new Exception("frameBody 数据异常");
//        }
        msg.setMeasure_Point_ID(bf.getUnsignedByte(0));
        msg.setMeasureData_Num(num);
        List<VoltageDayMon_Data> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            VoltageDayMon_Data item = new VoltageDayMon_Data();
            item.setStat_Time(bf.getIntLE(2 + i * 41) & 0xFFFFFFFF);
            byte[] DQualified_rate = new byte[3];
            bf.getBytes(6 + i * 41, DQualified_rate, 0, 3);
            item.setDQualified_rate(DQualified_rate);
            byte[] DOvercap_rate = new byte[3];
            bf.getBytes(9 + i * 41, DOvercap_rate, 0, 3);
            item.setDOvercap_rate(DOvercap_rate);
            byte[] DUnderlower_rate = new byte[3];
            bf.getBytes(12 + i * 41, DUnderlower_rate, 0, 3);
            item.setDUnderlower_rate(DUnderlower_rate);
            item.setDStatistical_time(bf.getUnsignedShortLE(15));
            item.setDQualified_time(bf.getUnsignedShortLE(17));
            item.setDOvercap_time(bf.getUnsignedShortLE(19));
            item.setDUnderlower_time(bf.getUnsignedShortLE(21));
            byte[] DAverage_value = new byte[4];
            bf.getBytes(23, DAverage_value);
            item.setDAverage_value(DAverage_value);
            byte[] DMini_value = new byte[4];
            bf.getBytes(27, DMini_value);
            item.setDMini_value(DMini_value);
            item.setDMini_value_Time(bf.getIntLE(31) & 0xFFFFFFFF);
            byte[] DMax_value = new byte[4];
            bf.getBytes(35, DMax_value);
            item.setDMax_value(DMax_value);
            item.setDMax_value_time(bf.getIntLE(39) & 0xFFFFFFFF);

            list.add(item);
        }
        msg.setMeasureData(list);
        return msg;
    }
}
